﻿$(function () {
    var pickerOpts = {
        changeMonth: true,
        changeYear: true,
        dateFormat: "yy/mm/dd",
        dayNamesMin: ["日", "一", "二", "三", "四", "五", "六"],
        firstDay: 0,
        nextText: "下一月",
        prevText: "上一月",
        monthNamesShort: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        isRTL: false,
        showButtonPanel: false,
        yearRange: "-25:+25"
    }
    $('#startTime').datepicker(pickerOpts);
    $('#endTime').datepicker(pickerOpts);
    $("tr.AlternatingRowStyle").live('dblclick', function () {
        //var ID = $(this).children().first().text().trim();
        var reqStatus = $.trim($('td.requestStatus', $(this)).html());
        var view;
        if (reqStatus == '已拒绝') {
            view = 'update';
        } else {
            view = 'views';
        }
        var ID = $.trim($(this).children().first().html());
        var rnd = (new Date()).getMilliseconds() + "" + parseInt(Math.random() * 10000);
        window.open("/ITService/" + view + "?servicerequestid=" + ID + "&nocache=" + rnd, "_self");
    });

    $("#search_submit").bind("click", function () {
        $("input#index").val(1);
        getmyWorkList(1);
    });

    $("#Submission").change("click", function () {
        getmyWorkList(1);
        $.ajax({
            url: "../WorkList/GetStatusFromEnum",
            type: "GET",
            data: { "val": $("select#Submission option:selected").val() },
            success: function (data) {
                $("#status").empty();
                $("#status").append("<option value=" + 0 + ">" + "---全部---" + "</option>");
                $.each(data, function (i, item) {
                    var n = i + 1;
                    $("#status").append("<option value=" + n + ">" + item.valueOf(i) + "</option>");
                });
            }
        });
    });

});
function getmyWorkList(stype) {
    var dater = (new Date()).toString() + Math.random() * 999;

    $.ajax({
        url: "getMyWorkList_By_params?r=" + dater,
        type: "GET",
        data: { "requestID": $("input#requestID").val(),
            "description": $("input#description").val(),
            "status": $("select#status option:selected").val(),
            "startTime": $("input#startTime").val(),
            "endTime": $("input#endTime").val(),
            "ps": parseInt($("input#size").val(), 10),
            "pi": parseInt($("input#index").val(), 10)
        },
        dataType: "json",
        success: function (data) {
            $("#List").empty();
            if ($("select#Submission").val() != 2) {
                $("#List").append("<tr><th>申请单号</th><th>事件单号</th><th>申请单描述</th><th>申请单状态</th><th>当前处理人</th><th>提交时间</th></tr>");
                $.each(data.Data.list, function (i, item) {
                    var Status;
                    switch (item.stauts) {
                        case 1:
                            Status = "审批中";
                            break;
                        case 2:
                            Status = "等待中";
                            break;
                        case 3:
                            Status = "处理中";
                            break;
                        case 4:
                            Status = "已完成";
                            break;
                        case 5:
                            Status = "已拒绝";
                            break;
                        case 6:
                            Status = "已取消";
                            break;
                    }
                    var desc = "";
                    if (item.description.toString().length > 0) {
                        if (item.description.toString().length > 20) {
                            desc += item.description.toString().substring(0, 17);
                            desc += "...";
                        }
                        else {
                            desc = item.description.toString();
                        }
                    }
                    else {
                        if (item.BDDescription.toString().length > 20) {
                            desc += item.BDDescription.toString().substring(0, 17);
                            desc += "...";
                        }
                        else {
                            desc = item.BDDescription.toString();
                        }
                    }

                    if (item.CurrentProcessor != null && item.CurrentProcessor.length > 0) {
                        var CurrPro = "";
                        if (item.stauts != 4 && item.stauts != 5 && item.stauts != 6) {
                            CurrPro = item.CurrentProcessor;
                        }
                        else {
                            CurrPro = "无";
                        }
                    }
                    else {
                        CurrPro = "无";
                    }

                    $("<tr class=\"AlternatingRowStyle\"><td>" + item.serviceRequestID + "</td><td>" + item.IncidentNumber + "</td><td>" + desc + "</td><td class='requestStatus'>" + Status + "</td><td>" + CurrPro + "</td><td>" + ConvertJSONDateToJSDateObject(item.createTime) + "</td></tr>").insertAfter($("#List tr:eq(" + i + ")"));
                });
            }
            else {
                $("#List").append("<tr><th>事件单号</th><th>申请单描述</th><th>申请单状态</th><th>当前处理人</th><th>提交时间</th></tr>");
                $.each(data.Data.list, function (i, item) {
                    var Status;
                    switch (item.stauts) {
                        case 1:
                            Status = "已指派";
                            break;
                        case 2:
                            Status = "进行中";
                            break;
                        case 3:
                            Status = "暂停";
                            break;
                        case 4:
                            Status = "已解决";
                            break;
                        case 5:
                            Status = "已关闭";
                            break;
                        case 6:
                            Status = "已取消";
                            break;

                    }
                    var desc = "";
                    if (item.description.toString().length > 20) {
                        desc += item.description.toString().substring(0, 17);
                        desc += "...";
                    }
                    else {
                        desc = item.description.toString();
                    }

                    if (item.CurrentProcessor != null && item.CurrentProcessor.length > 0) {
                        var CurrPro = "";
                        if (item.stauts != 4 && item.stauts != 5 && item.stauts != 6) {
                            CurrPro = item.CurrentProcessor;
                        }
                        else {
                            CurrPro = "无";
                        }
                    }
                    else {
                        CurrPro = "无";
                    }

                    $("<tr class=\"AlternatingRowStyle\"><td>" + item.IncidentNumber + "</td><td>" + desc + "</td><td class='requestStatus'>" + Status + "</td><td>" + CurrPro + "</td><td>" + ConvertJSONDateToJSDateObject(item.createTime) + "</td></tr>").insertAfter($("#List tr:eq(" + i + ")"));
                });
            }


            document.getElementById('itemCount').value = data.Data.count;
            if (stype == 1) {
                var pager = new PagerView('pager');
                pager.index = parseInt($("input#index").val(), 10);
                pager.itemCount = data.Data.count;
                pager.size = parseInt($("input#size").val(), 10);
                pager.onclick = function (index) {
                    $("input#index").val(index);
                    getmyWorkList(2);
                };
                pager.render();
            }
        }
    });
}
